<?php
/**********************************************************************
  Dragonfly CMS, Copyright (c) 2004 by CPGNuke Dev Team
  http://dragonflycms.org
  Released under GNU GPL version 2 or any later version

  $Source$
  $Revision$
  $Author$
  $Date$

  A free program released under the terms and conditions
  of the GNU GPL version 2 or any later version

  Linking CPG Dragonfly™ CMS statically or dynamically with other modules is making a
  combined work based on CPG Dragonfly CMS.  Thus, the terms and conditions of the GNU
  General Public License cover the whole combination.

  As a special exception, the copyright holders of CPG Dragonfly CMS give you
  permission to link CPG Dragonfly CMS with independent modules that communicate with
  CPG Dragonfly CMS solely through the CPG-Core interface, regardless of the license
  terms of these independent modules, and to copy and distribute the
  resulting combined work under terms of your choice, provided that
  every copy of the combined work is accompanied by a complete copy of
  the source code of CPG Dragonfly CMS (the version of CPG Dragonfly CMS used to produce the
  combined work), being distributed under the terms of the GNU General
  Public License plus this exception.  An independent module is a module
  which is not derived from or based on CPG Dragonfly CMS.

  Note that people who make modified versions of CPG Dragonfly CMS are not obligated
  to grant this special exception for their modified versions; it is
  their choice whether to do so.  The GNU General Public License gives
  permission to release a modified version without this exception; this
  exception also makes it possible to release a modified version which
  carries forward this exception.
  http://gnu.org/licenses/gpl-faq.html#LinkingOverControlledInterface

***********************************************************************/

# control css and js caching, not to confuse with CPG_DEBUG
define('DF_MODE_DEVELOPER', true);

# Start the chronometer
define('START_TIME', microtime(true));
# We also want to calculate server performance, from when the client first hit the server
define('START_REQUEST_TIME', !empty($_SERVER['REQUEST_TIME']) ? $_SERVER['REQUEST_TIME'] : 0); // timestamp, if PHP54 float with microseconds
# get current mem usage
define('START_MEMORY_USAGE', memory_get_usage());
$start_mem = START_MEMORY_USAGE;

# clean up
foreach ($_REQUEST AS $key => $val) {
	if (isset($$key)) unset($$key);
}
$_REQUEST = array();

# as usual
error_reporting(E_ALL | E_STRICT);

# ingore abort requests to allow clean shutdowns
ignore_user_abort(1);

# must set a time zone
date_default_timezone_set('UTC');

# register utf8 as the internal encoding
if (function_exists('mb_language')) { mb_language('uni'); }
if (function_exists('mb_internal_encoding')) { mb_internal_encoding('UTF-8'); }
//mb_regex_encoding('UTF-8');

# Core properties
define('CPG_NUKE', '10.0.9.35');
define('DS', DIRECTORY_SEPARATOR);
define('BASEDIR', dirname(__FILE__). DS);
define('CORE_PATH',    BASEDIR. 'includes'. DS);
define('ADMIN_PATH',   BASEDIR.    'admin'. DS);
define('CACHE_PATH',   BASEDIR.    'cache'. DS);
define('CLASS_PATH', CORE_PATH.  'classes'. DS);
define('MODULE_PATH',  BASEDIR.  'modules'. DS);
define('DF_LOG_FILE', CACHE_PATH.'error.log');
umask(0);

# Clear any previous buffering.
// ++$i or --$i wont change anything since ob_end_clean will always end_clean from last to first
for ($i=0, $c=ob_get_level(); $i<$c; ++$i) { ob_end_clean(); }

require_once(CORE_PATH.'core.inc');

# Init the engine and register it with PHP
$DF = new DF;
spl_autoload_register(array($DF, 'autoLoad'));
register_shutdown_function(array($DF, 'detachAll'));

if (DF_MODE_DEVELOPER) {
	$PerformanceLog = new PerformanceLog;
	$DF->attach('PerformanceLog');
}

function between($val, $min, $max) { return intval($val) >= intval($min) && intval($val) <= intval($max); }

# Load general filters
require_once(CLASS_PATH.'filter.php');

if (version_compare(phpversion(), '5.3', '<'))
{
	exit('You need atleast PHP 5.3 for this release. http://dragonflycms.org/News/article/sid=254.html');
}

# Define our error handled asap to also catch early errors
require_once(CLASS_PATH.'cpg_debugger.php');
//$DF->attach($cpgdebugger);

# load Poodle framework
require_once(CORE_PATH.'poodle/bootstrap.php');

# Try to start with our favorite settings
require_once(CLASS_PATH.'php.php');

if ('cli' === PHP::$run_as) {
	//require(CORE_PATH. 'load/cli.php');
	print 'Mode not supported, yet.';
	exit(1);
}

$DF->setState(DF::BOOT_CORE);

require_once(CORE_PATH.'dragonfly'.DS.'/net/http.php');
require_once(CLASS_PATH.'url.php');

# Continue creating constants
define('GZIPSUPPORT', extension_loaded('zlib'));
define('GZIP_OUT', GZIPSUPPORT && !ini_get('zlib.output_compression') && isset($_SERVER['HTTP_ACCEPT_ENCODING']) && false !== strpos($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip'));
define('WINDOWS', WINDOWS_OS);
define('PHPVERS', PHP::$version);
define('DOMAIN_PROTOCOL', (isset($_SERVER['HTTPS']) || 443 == $_SERVER['SERVER_PORT']) ? 'https' : 'http');
define('DOMAIN_NAME', str_replace('www.', '', empty($_SERVER['HTTP_HOST']) ? $_SERVER['SERVER_NAME'] : $_SERVER['HTTP_HOST']));
// if (!DOMAIN_NAME) trigger_error('Domain name not set, cannot proceed.', E_USER_ERROR);
//define('DOMAIN_NAME', isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : $MAIN_CFG['server']['domain']);
define('DOMAIN_PATH', Poodle::$URI_BASE.'/');

// Poodle_load_config()
$config_file = file_exists(CORE_PATH. 'config_'. DOMAIN_NAME. '.php') ? CORE_PATH. 'config_'. DOMAIN_NAME. '.php' :
			(file_exists(CORE_PATH. 'config.php') ? CORE_PATH. 'config.php' : false);
if ($config_file) {
	require_once($config_file);
	if (!defined('DF_MODE_INSTALL')) {
		exit('Invalid config.php, more details at http://dragonflycms.org/Wiki/id=135/');
	}
} else {
	$loader = 'install';
}
define('DOMAIN_INDEX', $mainindex);
define('DOMAIN_ADMIN', '?'. str_replace('.php', '', $adminindex));
define('BASEHREF', DOMAIN_PROTOCOL. '://'. DOMAIN_NAME. DOMAIN_PATH);
define('DF_SESSION_NAME', 'DF-' .sprintf("%u", crc32(Dragonfly_Net::ipn().BASEHREF)));
$adminindex = DOMAIN_ADMIN;

#
# HTTP stuff
#

if (is_array($_POST)) { array_walk($_POST, 'Prepare_INPUT'); }
if (is_array($_GET)) {
	//array_walk($_GET, 'Prepare_INPUT');
	Filter::_get();
}
//if (is_array($_COOKIE)) { array_walk($_COOKIE, 'Prepare_INPUT'); }

$loader = !empty($_GET) ? (string) key($_GET) : 'name';
$loader = empty($loader) || 'newlang' == $loader ? 'name' : $loader;
if (!preg_match('#^[a-z0-9_]+$#', $loader) || !is_file(CORE_PATH. 'load/'. $loader. '.php')) {
	Dragonfly_Net_Http::headersFlush(404, 'Page not found.');
}

header('Date: '. date('D, d M Y H:i:s', time()). ' GMT');
header('X-Powered-By: Dragonfly CMS using PHP engine');

if (empty($_SERVER['REQUEST_METHOD']) || 0 === preg_match('#^(?:HEAD|GET|POST)$#', $_SERVER['REQUEST_METHOD']) ) {
	Dragonfly_Net_Http::headersPush(405);
	Dragonfly_Net_Http::headersFlush('Allow: HEAD, GET, POST');
}
if (!is_file(CORE_PATH. 'load/'. $loader. '.php')) {
	Dragonfly_Net_Http::headersFlush(404, 'Page not found.');
}
if (!defined('INSTALL')) {
	if ($loader === 'admin') define('ADMIN_PAGES', true);
}

include_once(CORE_PATH. 'load/'. $loader. '.php');
exit;
